package com.xframework.model;

import com.xframework.Base;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by xuning on 2021/1/13.
 */
public class SqlParams extends BaseModel {

    private StringBuilder sb;

    private List params;

    private Class clazz;

    public SqlParams(String sql, BaseModel model, @Nonnull SqlParamSeg... sqlParamSegs) {
        this.sb = new StringBuilder(sql);
        this.params = new ArrayList();
        for (SqlParamSeg sqlParamSeg : sqlParamSegs) {
            if (sqlParamSeg.getExpress()) {
                this.sb.append(sqlParamSeg.getSql()).append(Base.SPACE);
                Object[] params = sqlParamSeg.getParams();
                if (params != null) {
                    for (Object param : params) {
                        this.params.add(param);
                    }
                }
            }
        }
        this.clazz = model.getClass();
        this.setPageIndex(model.getPageIndex());
        this.setPageSize(model.getPageSize());
    }

    public String sql() {
        return this.sb.toString();
    }

    public List params() {
        return this.params;
    }

    public Class clazz() {
        return this.clazz;
    }

    public SqlParams addSql(String sql) {
        this.sb.append(sql);
        return this;
    }

    public SqlParams addParam(Object param) {
        this.params.add(param);
        return this;
    }

    public SqlParams addSqlParams(String sql, @Nonnull Object... params) {
        for (Object param : params) {
            if (!Base.isNotNull(param)) {
                return this;
            }
        }
        sb.append(sql);
        for (Object param : params) {
            this.params.add(param);
        }
        return this;
    }

}
